﻿function GetGoods() {
    GoodsInfo.innerHTML = "";


    let AddGood = document.createElement("div");
    AddGood.className = "AddGood";
    AddGood.innerHTML = `     <div class="Icon">
            <i class="fas fa-plus-circle"></i>
        </div>
        <div class="Text">添加商品</div>`;


    AddGood.onclick = function () {
        window.location.href = "/Goods/AddGoodsHtml";
    }

    GoodsInfo.appendChild(AddGood);


    $.get("/Goods/GetGoods", { Glid: dto.Glid, status: dto.status, key: dto.key }).then((res) => {
        res.data.forEach((item, e) => {

            let option = document.createElement("div");
            option.className = "option";

            let Setting = document.createElement("div");
            Setting.className = "Setting";

            let icon = document.createElement("i");
            icon.className = "fas fa-ellipsis-v";


            Setting.appendChild(icon);



            let Control_Button = document.createElement("div");
            Control_Button.className = "Control-Button";



            let Detials = document.createElement("button");
            Detials.innerHTML = "详情";

            if (item.status == 1) {
                let PutShelf = document.createElement("button");
                PutShelf.innerHTML = "上架";
                Control_Button.appendChild(PutShelf);

                PutShelf.onclick = function () {

                    $.post("/Goods/UpdateGoodStatus", { id: item.id, status: 0 }).then((res) => {
                        console.log(res);


                        Input.value = "";
                        dto.key = Input.value;
                        Peng.Show("上架成功", { type: 'Success' });

                        GetGoods();
                    });
                }


            } else {
                let OutShelf = document.createElement("button");
                OutShelf.innerHTML = "下架";
                Control_Button.appendChild(OutShelf);

                OutShelf.onclick = function () {




                    $.post("/Goods/UpdateGoodStatus", { id: item.id, status: 1 }).then((res) => {
                        Input.value = "";
                        dto.key = Input.value;
                        Peng.Show("下架成功", { type: 'Success' });
                        GetGoods();
                    });
                }
            }

            Detials.onclick = function () {
                sessionStorage.setItem('id', item.id);
                location.href = "/Goods/GoodDetailsHtml";
            }
            Control_Button.appendChild(Detials);

            Setting.appendChild(Control_Button);

            let divimg = document.createElement("div");
            divimg.className = "img";

            let img = document.createElement("img");
            img.src = item.img;
            divimg.appendChild(img);

            let Good_Message = document.createElement("div");
            Good_Message.className = "Good_Message";

            let GoodName = document.createElement("div");
            GoodName.className = "GoodName";
            GoodName.innerHTML = item.name;
            let GoodDetials = document.createElement("div");
            GoodDetials.className = "GoodDetials";
            GoodDetials.innerHTML = item.detail;
            let GoodPrice = document.createElement("div");
            GoodPrice.className = "GoodPrice";
            GoodPrice.innerHTML = `￥${item.price}`;
            Good_Message.appendChild(GoodName);
            Good_Message.appendChild(GoodDetials);
            Good_Message.appendChild(GoodPrice);



            option.appendChild(Setting);
            option.appendChild(divimg);
            option.appendChild(Good_Message);
            let GoodsNumber = document.createElement("div");
            GoodsNumber.className = "GoodsNumber";

            let StatusName = document.createElement("div");
            StatusName.className = "StatusName";


            let Status = document.createElement("div");
            Status.className = "Status";
            switch (item.status) {
                case 0:
                    Status.innerHTML = "已上架";
                    Status.classList.add("YSJ");
                    break;
                case 1:
                    Status.innerHTML = "未上架";
                    Status.classList.add("WSJ");
                    break;
                case 2:
                    Status.innerHTML = "已售馨";
                    Status.classList.add("YSX");
                    break;
                case 3:
                    Status.innerHTML = "库存不足";
                    Status.classList.add("KCBZ");
                    break;

            }



            let KuCun = document.createElement("div");
            KuCun.className = "KuCun";
            KuCun.innerHTML = `  <div>
                    库存 ：
                </div>
                <div class="Number">
                    ${item.num}
                </div>`;

            StatusName.appendChild(Status);

            GoodsNumber.appendChild(StatusName);
            GoodsNumber.appendChild(KuCun);


            option.appendChild(GoodsNumber);

            GoodsInfo.appendChild(option);
        });
    });


}


let Input = document.querySelector("#GoodName");

let Status = [{
    status: -1,
    name: '全部'
}, {
    status: 0,
    name: '已上架'
}, {
    status: 1,
    name: '未上架'
}];

let dto = {
    Glid: '',
    status: -1,
    key: ''
};

let GoodsInfo = document.querySelector(".GoodsInfo .Goods .GoodsBox .Goods_Info");
Peng_Select.CreateElement('Status_Select', Status, 'name', (res) => {
    dto.status = res.status;
    Input.value = "";
    dto.key = Input.value;
    GetGoods();
});

var Query = function () {

    dto.key = Input.value;
    GetGoods();
    Input.value = "";
}


// 商品类型 盒子
let GoodsTypeBox = document.querySelector(".GoodsInfo .GoodsType .GoodsTypeBox");
// 商品类型 滑块
let GoodsTypeSelect = document.querySelector(".GoodsInfo .GoodsType .GoodsTypeBox .Select");


// 商品标签 盒子
let GoodsLabelBox = document.querySelector(".GoodsInfo .Goods .GoodsBox .Good_Label .GoodsLabelBox");



function getGoodsType() {
    GoodsTypeBox.innerHTML = "";


    let Select = document.createElement("div");
    Select.className = "Select";
    GoodsTypeBox.appendChild(Select);

    let all = document.createElement("div");
    all.className = "all";
    all.innerHTML = ` <div class="img"><img src="/Source/good.png"></div>
            <div class="Name">所有类型</div>
            <div class="Icon">
                <i class="fas fa-globe-americas"></i>
            </div>`;

    all.onclick = function () {
        let options = GoodsTypeBox.querySelectorAll(".option");

        options.forEach((item) => {
            item.classList.remove("active");
        });
        all.classList.add("active");

        Select.style.transform = `translateY(${0}px)`;


        getGoodsLabel("");

    }


    all.click();
    GoodsTypeBox.appendChild(all);



    $.get("/Goods/GetGoodsType").then((res) => {
        res.data.forEach((item, e) => {
            let option = document.createElement("div");
            option.className = "option";

            let divimg = document.createElement("div");
            divimg.className = "img";

            let img = document.createElement("img");
            img.src = item.img;
            divimg.appendChild(img);
            let Name = document.createElement("div");
            Name.className = "Name";
            Name.innerHTML = item.name;

            let Help = document.createElement("div");
            Help.className = "Help";


            let i = document.createElement("i");
            i.className = "fas fa-ellipsis-v";
            Help.appendChild(i);

            let Control_Button = document.createElement("div");
            Control_Button.className = "Control-Button";

            let Edit = document.createElement("button");
            Edit.innerHTML = "编辑";

            Edit.onclick = function (e) {
                e.stopPropagation();
                let obj =
                {
                    id: item.id,
                    name: item.name,
                    img: item.img
                }

                sessionStorage.setItem("obj", JSON.stringify(obj));

                location.href = "/Goods/UpdateGoodTypeHtml";

            }



            let Details = document.createElement("button");
            Details.innerHTML = "详情";

            Details.onclick = function (e) {
                e.stopPropagation();

                sessionStorage.setItem("id", item.id);
                location.href = "/Goods/GoodsLabelManager";
            }

            Control_Button.appendChild(Edit);
            Control_Button.appendChild(Details);
            Help.appendChild(Control_Button);
            option.appendChild(divimg);
            option.appendChild(Name);
            option.appendChild(Help);


            option.onclick = function () {
                let options = GoodsTypeBox.querySelectorAll(".option");
                all.classList.remove("active");

                options.forEach((item) => {
                    item.classList.remove("active");
                });
                option.classList.add("active");

                Select.style.transform = `translateY(${(e + 1) * 60}px)`;
                getGoodsLabel(item.id);
            }

            GoodsTypeBox.appendChild(option);

        });
    });



}

getGoodsType();


function getGoodsLabel(id) {
    GoodsLabelBox.innerHTML = "";


    let Select = document.createElement("div");
    Select.className = "Select";
    GoodsLabelBox.appendChild(Select);

    let all = document.createElement("div");
    all.className = "option";

    let allName = document.createElement("div");
    allName.className = "Name";
    allName.innerHTML = "所有标签";
    let allHelp = document.createElement("div");
    allHelp.className = "Help";

    let ii = document.createElement("i");
    ii.className = "fas fa-globe-americas";

    allHelp.appendChild(ii);
    all.appendChild(allName);
    all.appendChild(allHelp);




    all.onclick = function () {
        let options = GoodsLabelBox.querySelectorAll(".option");
        options.forEach((item) => {
            item.classList.remove("active");
        });
        all.classList.add("active");
        Select.style.transform = `translateY(${0}px)`;
        Input.value = "";
        dto.key = Input.value;
        dto.Glid = '';
        GetGoods();
    }
    all.click();
    GoodsLabelBox.appendChild(all);




    $.get("/Goods/GetGoodslabelByGTid", { id: id }).then(res => {
        res.data.forEach((item, e) => {
            let option = document.createElement("div");
            option.className = "option";

            let Name = document.createElement("div");
            Name.className = "Name";
            Name.innerHTML = item.name;
            let Help = document.createElement("div");
            Help.className = "Help";

            let i = document.createElement("i");
            i.className = "fas fa-ellipsis-v";

            Help.appendChild(i);

            let Control_Button = document.createElement("div");
            Control_Button.className = "Control-Button";

            let Edit = document.createElement("button");
            Edit.innerHTML = "编辑";
            let Detials = document.createElement("button");
            Detials.innerHTML = "详情";




            Control_Button.appendChild(Edit);

            Control_Button.appendChild(Detials);

            Help.appendChild(Control_Button);

            option.appendChild(Name);
            option.appendChild(Help);
            option.onclick = function () {
                let options = GoodsLabelBox.querySelectorAll(".option");
                options.forEach((item) => {
                    item.classList.remove("active");
                });
                option.classList.add("active");
                Select.style.transform = `translateY(${(e + 1) * 60}px)`;


                Input.value = "";
                dto.key = Input.value;
                dto.Glid = item.id;
                GetGoods();

            }

            GoodsLabelBox.appendChild(option);
        });
    });
}
















